<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\paths\controller;

use app\common\model\paths\EmployModel;
use app\common\model\paths\TaskChapterModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\facade\Validate;
use think\file\UploadedFile;

/**
 * 课程章节控制器
 * @class Base
 * @package app\admin\controller
 */
class TaskChapter extends Controller
{

    /**
     * 章节列表
     * @auth true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {
        $this->task_id = $this->request->get('task_id');

        TaskChapterModel::mQuery()->layTable(function () {

        }, function (QueryHelper $query) {


            $this->check_account = false;
            $this->check_user    = false;

            $query->where('task_id', $this->task_id);
            // 数据列表搜索过滤
            $query->like('title')->equal('status')->dateBetween('create_at');
        });
    }

    protected function _index_page_filter (&$data)
    {


        // 菜单数据树数据变平化
        $data = DataExtend::arr2table($data);

        foreach ($data as &$item) {

            if ($item['pid'] !== 0) {
                $item['title'] = 'ㅤ├ㅤ'.$item['title'];
            }
        }

    }
    /**
     * 添加
     *
     * @return void
     */
    public function add ()
    {
        TaskChapterModel::mForm();
    }

    /**
     * 添加
     *
     * @return void
     */
    public function addSon ()
    {
        TaskChapterModel::mForm();
    }

    /**
     * 表单数据处理
     * @param array $vo
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isGet()) {
            $this->task_id = $this->request->get('task_id');
            //$this->type_arr = EmployInfoModel::getTypeName();
            if (!empty($vo['employ_id'])) {
                $vo['employ_name'] = EmployModel::mk()->where('id', $vo['employ_id'])->value('title');

            }
        }else{
            if(in_array($this->request->action(), ['addSon','edit'])){
                $rule = [
                    'title|章节名称'   => 'require|length:1,255',
                    'details|简介'     => 'length:1,255',
                ];
                if(!empty($vo['pid'])){
                    $rule += [
                        'type|类型'       => 'require|in:1,2,3',
                    ];
                    if(in_array($vo['type'], [1,2])){
                        $rule += [
                            'content|上传文件'      =>  'require',
                            'duration|学习时长'     =>  'require|float|between:0.01,1000000',
                            'score|学习分数'        =>  'require|integer|between:1,10000'
                        ];
                    }else{
                        $rule += [
                            'employ_id|绑定实战项目'        =>  'require|number|between:0,10000'
                        ];
                    }
                }
                $validate = Validate::rule($rule);
                if (!$validate->check($vo)) {
                    $this->error($validate->getError());
                }
            }
        }
    }
    /**
     * 编辑
     *
     * @return void
     */
    public function edit ()
    {
        TaskChapterModel::mForm();
    }


    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {

        TaskChapterModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {

        TaskChapterModel::mDelete();
    }

    /**
     * 分片上传
     *
     * @return void
     */
    public function upload ()
    {
        $file = $this->request->file('file');
        //$chunkCount = $this->request->post('chunkCount');
        $indexCount = $this->request->post('indexCount');
        $fileName   = $this->request->post('fileName');

        $paths = "upload/".date('Ymd');
        if ($indexCount == 1) { // 第一个碎片
            $file->move(public_path($paths), $fileName);
        } else {

            $file_info = file_get_contents($file->getPathname());
            file_put_contents(public_path($paths).'/'.$fileName, $file_info, FILE_APPEND);
        }

        $this->success("成功", "{$paths}/{$fileName}");
    }

    /**
     * 获得实战项目列表
     *
     * @return void
     */
    public function getEmployList()
    {

        EmployModel::mQuery()->layTable(function () {

            $this->task_info_id = $this->request->get('task_info_id');

        }, function (QueryHelper $query) {

            $query->where('status', 1);
            // 数据列表搜索过滤
            $query->like('title')->dateBetween('create_at');
        }, 'employ');
    }

    /**
     * 确认关联
     *
     * @return void
     */
    public function checkedEmploy()
    {
        $task_info_id = $this->request->post('task_info_id');
        $employ_id = $this->request->post('id');

        EmployModel::where('id', $task_info_id)->update(['employ_id'=>$employ_id]);

        $this->success('成功');
    }

}